package com.dji.sample.wayline.controller;

import com.dji.sample.common.model.*;
import com.dji.sample.common.util.DjKmlUtil;
import com.dji.sample.common.util.MinioUtil;
import com.dji.sample.component.redis.RedisConst;
import com.dji.sample.component.redis.RedisOpsUtils;
import com.dji.sample.manage.model.dto.DeviceDTO;
import com.dji.sample.manage.model.enums.DeviceDomainEnum;
import com.dji.sample.manage.service.IDeviceService;
import com.dji.sample.wayline.model.dto.UploadKmlDto;
import com.dji.sample.wayline.model.dto.WaylineFileDTO;
import com.dji.sample.wayline.model.dto.WaylineFileUploadDTO;
import com.dji.sample.wayline.model.entity.WaylineInfoEntity;
import com.dji.sample.wayline.model.param.WaylineQueryParam;
import com.dji.sample.wayline.service.IWaylineFileService;
import io.minio.ObjectWriteResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;

import static com.dji.sample.component.AuthInterceptor.TOKEN_CLAIM;

/**
 * @author sean
 * @version 0.3
 * @date 2021/12/22
 */
@RestController
@RequestMapping("${url.wayline.prefix}/api/v2/workspaces")
public class WaylineFileV2Controller {

    @Autowired
    private IWaylineFileService waylineFileService;

    @Autowired
    MinioUtil minioUtil;

    @Autowired
    DjKmlUtil kmlUtil;

    Random random = new Random();

    @Autowired
    private RedisOpsUtils redisOps;

    @Autowired
    IDeviceService deviceService;

    @PostMapping("/uploadKml")
    public ResponseResult<?> uploadKml(@RequestBody UploadKmlDto travelRecords, HttpServletRequest request) throws Exception {
        //生成航线
        boolean al = StringUtils.hasText(travelRecords.getTakeOffSecurityHeight());
        if (!al) {
            return ResponseResult.error("起飞高度参数异常");
        }
        boolean a2 = StringUtils.hasText(travelRecords.getEllipsoidHeight());
        if (!a2) {
            return ResponseResult.error("航线速度参数异常");
        }
        boolean a3 = StringUtils.hasText(travelRecords.getSpeed());
        if (!a3) {
            return ResponseResult.error("全局速度参数异常");
        }
        CustomClaim customClaim = (CustomClaim) request.getAttribute(TOKEN_CLAIM);
        travelRecords.setOrgCode(customClaim.getOrgCode());
        travelRecords.setOrgId(customClaim.getOrgId());
        travelRecords.setUsername(customClaim.getUsername());
        if (travelRecords.getTravelRecordRequests().isEmpty()) {
            return ResponseResult.error("点位为空");
        }
        ObjectWriteResponse objectWriteResponse = kmlUtil.setTravelsV2Kml(travelRecords, UUID.randomUUID().toString());
        if (objectWriteResponse == null) {
            return ResponseResult.error("上传失败");
        }
        waylineFileService.saveBatch(travelRecords, objectWriteResponse);
        return ResponseResult.success(objectWriteResponse.object());
    }



    @GetMapping("/getDetailById")
    public ResponseResult<?> getDetail(@RequestParam(name = "id") Long id) {
        return ResponseResult.success(waylineFileService.getDetailById(id));
    }

    @PostMapping("/update")
    public ResponseResult<?> update(@RequestBody UploadKmlDto travelRecords, HttpServletRequest request) throws Exception {
        //生成航线
        if (travelRecords.getId() == null) {
            return ResponseResult.error("id必传");
        }
        boolean al = StringUtils.hasText(travelRecords.getTakeOffSecurityHeight()) && Integer.parseInt(travelRecords.getTakeOffSecurityHeight()) > 0;
        if (!al) {
            return ResponseResult.error("起飞高度参数异常");
        }
        boolean a2 = StringUtils.hasText(travelRecords.getEllipsoidHeight()) && Integer.parseInt(travelRecords.getEllipsoidHeight()) > 0;
        if (!a2) {
            return ResponseResult.error("航线速度参数异常");
        }
        boolean a3 = StringUtils.hasText(travelRecords.getSpeed()) && Integer.parseInt(travelRecords.getSpeed()) > 0;
        if (!a3) {
            return ResponseResult.error("全局速度参数异常");
        }
        CustomClaim customClaim = (CustomClaim) request.getAttribute(TOKEN_CLAIM);
        travelRecords.setOrgCode(customClaim.getOrgCode());
        travelRecords.setOrgId(customClaim.getOrgId());
        travelRecords.setUsername(customClaim.getUsername());
        if (travelRecords.getTravelRecordRequests().isEmpty()) {
            return ResponseResult.error("点位为空");
        }
        waylineFileService.updateV2wayInfo(travelRecords);
        return ResponseResult.success("修改成功");
    }

}
